package com.wk;

import com.wk.Model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.testng.annotations.BeforeClass;

import javax.lang.model.element.ExecutableElement;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

public class Testq {

    private static SqlSessionFactory sqlSessionFactory;
    public static void init(){
        try {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //byte[] bytes = new byte[1024];
            //int len ;
            //while(( len = is.read(bytes,0,1024)) != -1){
              //  bos.write(bytes,0,len);
                //bos.flush();
            //}
            //bos.close();
            //byte[] end = bos.toByteArray();
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            is.close();
        }catch (Exception ex){
            System.out.println(ex.toString());
        }
    }


    public void testSelectAll(){
        init();
        try (SqlSession sqlSession = sqlSessionFactory.openSession()){
            List<User> list = sqlSession.selectList("selectAll");
            for (User user : list) {
                System.out.println(user.getUsername() + "  " + user.getPassword());
            }

        }
    }
    @Test
    public void testInsert(){
        init();
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            User user = new User();
            user.setId(0);
            user.setUsername("pppp");
            user.setPassword("123456");
            sqlSession.insert("insert",user);
        }catch (Exception ex){
            System.out.println(ex.toString());
        }
    }
}
